var ctx=window.daijia.ctx;

$(document).ready(function(){
	
	Highcharts.createElement('link', {
		href: 'http://fonts.googleapis.com/css?family=Dosis:400,600',
		rel: 'stylesheet',
		type: 'text/css'
	}, null, document.getElementsByTagName('head')[0]);

	Highcharts.theme = {
		colors: ["#7cb5ec", "#f7a35c", "#90ee7e", "#7798BF", "#aaeeee", "#ff0066", "#eeaaee",
			"#55BF3B", "#DF5353", "#7798BF", "#aaeeee"],
		chart: {
			backgroundColor: null,
			style: {
				fontFamily: "Dosis, sans-serif"
			}
		},
		title: {
			style: {
				fontSize: '16px',
				fontWeight: 'bold',
				textTransform: 'uppercase'
			}
		},
		tooltip: {
			borderWidth: 0,
			backgroundColor: 'rgba(219,219,216,0.8)',
			shadow: false
		},
		legend: {
			itemStyle: {
				fontWeight: 'bold',
				fontSize: '13px'
			}
		},
		xAxis: {
			gridLineWidth: 1,
			labels: {
				style: {
					fontSize: '12px'
				}
			}
		},
		yAxis: {
			minorTickInterval: 'auto',
			title: {
				style: {
					textTransform: 'uppercase'
				}
			},
			labels: {
				style: {
					fontSize: '12px'
				}
			}
		},
		plotOptions: {
			candlestick: {
				lineColor: '#404048'
			}
		},


		// General
		background2: '#F0F0EA'
		
	};

	// Apply the theme
	Highcharts.setOptions(Highcharts.theme);
	
	
	
	
	var newDate = new Date();
	var startDate = new Date();
	startDate.setDate(startDate.getDate()-30);
	var startTimeStr=startDate.getFullYear()+"-"+((startDate.getMonth()+1)>9?(startDate.getMonth()+1):'0' + (startDate.getMonth()+1))+"-"+(startDate.getDate()>9?startDate.getDate():'0' + startDate.getDate())+" "+"00:00";
	var endTimeStr=newDate.getFullYear()+"-"+((newDate.getMonth()+1)>9?(newDate.getMonth()+1):'0' + (newDate.getMonth()+1))+"-"+(newDate.getDate()>9?newDate.getDate():'0' + newDate.getDate())+" "+"00:00";
	$("#startTime").val(startTimeStr);
	$("#endTime").val(endTimeStr);
	$("#errandStartTime").val(startTimeStr);
	$("#errandEndTime").val(endTimeStr);
	$("#zhuancheStartTime").val(startTimeStr);
	$("#zhuancheEndTime").val(endTimeStr);
	$("#freightStartTime").val(startTimeStr);
	$("#freightEndTime").val(endTimeStr);
	$("#zhuanxianStartTime").val(startTimeStr);
	$("#zhuanxianEndTime").val(endTimeStr);
	
	$("#startTime").datetimepicker({
        format: "yyyy-mm-dd HH:ii",
        language: 'zh-CN',
        showMeridian: true,
        autoclose: true,
        todayBtn: true
    });
	$("#endTime").datetimepicker({
		format: "yyyy-mm-dd HH:ii",
        language: 'zh-CN',
        showMeridian: true,
        autoclose: true,
        todayBtn: true
    });
	$("#errandStartTime").datetimepicker({
        format: "yyyy-mm-dd HH:ii",
        language: 'zh-CN',
        showMeridian: true,
        autoclose: true,
        todayBtn: true
    });
	$("#errandEndTime").datetimepicker({
		format: "yyyy-mm-dd HH:ii",
        language: 'zh-CN',
        showMeridian: true,
        autoclose: true,
        todayBtn: true
    });
	$("#zhuancheStartTime").datetimepicker({
        format: "yyyy-mm-dd HH:ii",
        language: 'zh-CN',
        showMeridian: true,
        autoclose: true,
        todayBtn: true
    });
	$("#zhuancheEndTime").datetimepicker({
		format: "yyyy-mm-dd HH:ii",
        language: 'zh-CN',
        showMeridian: true,
        autoclose: true,
        todayBtn: true
    });
	$("#zhuanxianStartTime").datetimepicker({
		format: "yyyy-mm-dd HH:ii",
		language: 'zh-CN',
		showMeridian: true,
		autoclose: true,
		todayBtn: true
	});
	$("#zhuanxianEndTime").datetimepicker({
		format: "yyyy-mm-dd HH:ii",
		language: 'zh-CN',
		showMeridian: true,
		autoclose: true,
		todayBtn: true
	});
	$("#freightStartTime").datetimepicker({
        format: "yyyy-mm-dd HH:ii",
        language: 'zh-CN',
        showMeridian: true,
        autoclose: true,
        todayBtn: true
    });
	$("#freightEndTime").datetimepicker({
		format: "yyyy-mm-dd HH:ii",
        language: 'zh-CN',
        showMeridian: true,
        autoclose: true,
        todayBtn: true
    });
	
	
	//点击查询
	$("#errand_search_button").click(function(){
		errandLoad();
	});
	$("#zhuanche_search_button").click(function(){
		zhuancheLoad();
	});
	$("#freight_search_button").click(function(){
		// freightLoad();
	});
	$("#zhuanxian_search_button").click(function(){
		zhuanxianLoad();
	});
	$("#search_button").click(function(){
		daijiaLoad();
	});
	/*$("#companyId").change(function(){
		daijiaLoad();
		errandLoad();
		zhuancheLoad();
	 });*/
	
	daijiaLoad();
	errandLoad();
	zhuancheLoad();
	// freightLoad();
	zhuanxianLoad();

});

function errandLoad(){
	
	$.post(ctx+"/chart/errandOrderFromSourceDate",{'startTime':$("#errandStartTime").val(),'endTime':$("#errandEndTime").val(),'companyId':$("#errandCompanyId").val()},function(data){
        var record = new Array();
        var content = data.content;
        for(var i=0;i<data.totalElements;i++){
        	if(content[i].fromSource.indexOf("系统")!=-1){
        		record[i]={'name':content[i].fromSource,'y':content[i].amount/data.total,'sliced': true,'selected': true};
        	}else{
        		record[i]=[content[i].fromSource,content[i].amount/data.total];
        	}
             
        }
        
      //  load(record);
         var tr = "<tr style='font-size:14px;'><th>数量\\类型</th>";
	 	 var tr1="<tr><td>订单数量</td>";
	 	 var tr2="<tr><td>订单金额</td>";
         for(var i=0;i<data.totalElements;i++){
        	 
        	 if(i==data.totalElements-1){
        		 tr=tr+"<th>"+content[i].fromSource+"</th><th>总计</th></tr>";
        		 tr1=tr1+"<td>"+content[i].amount+" (单)</td><td>"+data.total+" (单)</td></tr>";
        		 tr2=tr2+"<td>"+content[i].money+" (元)</td><td>"+data.allTotal+" (元)</td><tr>"
        	 }else{
        		 tr=tr+"<th>"+content[i].fromSource+"</th>";
        		 tr1=tr1+"<td>"+content[i].amount+" (单)</td>";
        		 tr2=tr2+"<td>"+content[i].money+" (元)</td>"
        	 }
         }   
        $("#errandTable thead tr").remove();
		$("#errandTable thead").append(tr);
		$("#errandTable tbody tr").remove();
		$("#errandTable tbody").append(tr1+tr2);
		$("#errandTotal").text("共 "+data.totalElements+" 种类型");
		$('#errand').highcharts({
	        chart: {
	            plotBackgroundColor: null,
	            plotBorderWidth: null,
	            plotShadow: false
	        },
	        title: {
	            text: '跑腿订单类型统计'
	        },
	        tooltip: {
	    	    pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
	        },
	        plotOptions: {
	            pie: {
	                allowPointSelect: true,
	                cursor: 'pointer',
	                dataLabels: {
	                    enabled: true,
	                    color: '#000000',
	                    connectorColor: '#000000',
	                    format: '<b>{point.name}</b>: {point.percentage:.1f} %'
	                }
	            }
	        },
	        series: [{
	            type: 'pie',
	            name: '完成订单量',
	            data: record
	        
	        }]
	    });
	},"json");
	
}
	
function daijiaLoad(){
	
	$.post(ctx+"/chart/orderFromSourceDate",{'startTime':$("#startTime").val(),'endTime':$("#endTime").val(),'companyId':$("#companyId").val()},function(data){
        var record = new Array();
        var content = data.content;
        for(var i=0;i<data.totalElements;i++){
        	if(content[i].fromSource.indexOf("APP")!=-1){
        		record[i]={'name':content[i].fromSource,'y':content[i].amount/data.total,'sliced': true,'selected': true};
        	}else{
        		record[i]=[content[i].fromSource,content[i].amount/data.total];
        	}
             
        }
        
      //  load(record);
         var tr = "<tr style='font-size:14px;'><th>数量\\类型</th>";
	 	 var tr1="<tr><td>订单数量</td>";
	 	 var tr2="<tr><td>订单金额</td>";
         for(var i=0;i<data.totalElements;i++){
        	 if(i==data.totalElements-1){
        		 tr=tr+"<th>"+content[i].fromSource+"</th><th>总计</th></tr>";
        		 tr1=tr1+"<td>"+content[i].amount+" (单)</td><td>"+data.total+" (单)</td></tr>";
        		 tr2=tr2+"<td>"+content[i].money+" (元)</td><td>"+data.allTotal+" (元)</td><tr>"
        	 }else{
        		 tr=tr+"<th>"+content[i].fromSource+"</th>";
        		 tr1=tr1+"<td>"+content[i].amount+" (单)</td>";
        		 tr2=tr2+"<td>"+content[i].money+" (元)</td>"
        	 }
         }   
        $("#daijiaTable thead tr").remove();
		$("#daijiaTable thead").append(tr);
		$("#daijiaTable tbody tr").remove();
		$("#daijiaTable tbody").append(tr1+tr2);
		$("#daijiaTotal").text("共 "+data.totalElements+" 种类型");
		$('#daijia').highcharts({
	        chart: {
	            plotBackgroundColor: null,
	            plotBorderWidth: null,
	            plotShadow: false
	        },
	        title: {
	            text: '代驾订单类型统计'
	        },
	        tooltip: {
	    	    pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
	        },
	        plotOptions: {
	            pie: {
	                allowPointSelect: true,
	                cursor: 'pointer',
	                dataLabels: {
	                    enabled: true,
	                    color: '#000000',
	                    connectorColor: '#000000',
	                    format: '<b>{point.name}</b>: {point.percentage:.1f} %'
	                }
	            }
	        },
	        series: [{
	            type: 'pie',
	            name: '完成订单量',
	            data: record
	        
	        }]
	    });
	},"json");
	
}
function zhuancheLoad(){
	
	$.post(ctx+"/chart/zhuancheOrderFromSourceDate",{'startTime':$("#zhuancheStartTime").val(),'endTime':$("#zhuancheEndTime").val(),'companyId':$("#zhuancheCompanyId").val()},function(data){
        var record = new Array();
        var content = data.content;
        for(var i=0;i<data.totalElements;i++){
        	if(content[i].fromSource.indexOf("APP")!=-1){
        		record[i]={'name':content[i].fromSource,'y':content[i].amount/data.total,'sliced': true,'selected': true};
        	}else{
        		record[i]=[content[i].fromSource,content[i].amount/data.total];
        	}
             
        }
        
      //  load(record);
         var tr = "<tr style='font-size:14px;'><th>数量\\类型</th>";
	 	 var tr1="<tr><td>订单数量</td>";
	 	 var tr2="<tr><td>订单金额</td>";
         for(var i=0;i<data.totalElements;i++){
        	 if(i==data.totalElements-1){
        		 tr=tr+"<th>"+content[i].fromSource+"</th><th>总计</th></tr>";
        		 tr1=tr1+"<td>"+content[i].amount+" (单)</td><td>"+data.total+" (单)</td></tr>";
        		 tr2=tr2+"<td>"+content[i].money+" (元)</td><td>"+data.allTotal+" (元)</td><tr>"
        	 }else{
        		 tr=tr+"<th>"+content[i].fromSource+"</th>";
        		 tr1=tr1+"<td>"+content[i].amount+" (单)</td>";
        		 tr2=tr2+"<td>"+content[i].money+" (元)</td>"
        	 }
         }   
        $("#zhuancheTable thead tr").remove();
		$("#zhuancheTable thead").append(tr);
		$("#zhuancheTable tbody tr").remove();
		$("#zhuancheTable tbody").append(tr1+tr2);
		$("#zhuancheTotal").text("共 "+data.totalElements+" 种类型");
		$('#zhuanche').highcharts({
	        chart: {
	            plotBackgroundColor: null,
	            plotBorderWidth: null,
	            plotShadow: false
	        },
	        title: {
	            text: '专车订单类型统计'
	        },
	        tooltip: {
	    	    pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
	        },
	        plotOptions: {
	            pie: {
	                allowPointSelect: true,
	                cursor: 'pointer',
	                dataLabels: {
	                    enabled: true,
	                    color: '#000000',
	                    connectorColor: '#000000',
	                    format: '<b>{point.name}</b>: {point.percentage:.1f} %'
	                }
	            }
	        },
	        series: [{
	            type: 'pie',
	            name: '完成订单量',
	            data: record
	        
	        }]
	    });
	},"json");
	
}

function zhuanxianLoad(){
	
	$.post(ctx+"/chart/zhuanxianOrderFromSourceDate",{'startTime':$("#zhuanxianStartTime").val(),'endTime':$("#zhuanxianEndTime").val(),'companyId':$("#zhuanxianCompanyId").val()},function(data){
        var record = new Array();
        var content = data.content;
        for(var i=0;i<data.totalElements;i++){
        	if(content[i].fromSource.indexOf("APP")!=-1){
        		record[i]={'name':content[i].fromSource,'y':content[i].amount/data.total,'sliced': true,'selected': true};
        	}else{
        		record[i]=[content[i].fromSource,content[i].amount/data.total];
        	}
             
        }
        
      //  load(record);
         var tr = "<tr style='font-size:14px;'><th>数量\\类型</th>";
	 	 var tr1="<tr><td>订单数量</td>";
	 	 var tr2="<tr><td>订单金额</td>";
         for(var i=0;i<data.totalElements;i++){
        	 if(i==data.totalElements-1){
        		 tr=tr+"<th>"+content[i].fromSource+"</th><th>总计</th></tr>";
        		 tr1=tr1+"<td>"+content[i].amount+" (单)</td><td>"+data.total+" (单)</td></tr>";
        		 tr2=tr2+"<td>"+content[i].money+" (元)</td><td>"+data.allTotal+" (元)</td><tr>"
        	 }else{
        		 tr=tr+"<th>"+content[i].fromSource+"</th>";
        		 tr1=tr1+"<td>"+content[i].amount+" (单)</td>";
        		 tr2=tr2+"<td>"+content[i].money+" (元)</td>"
        	 }
         }   
        $("#zhuanxianTable thead tr").remove();
		$("#zhuanxianTable thead").append(tr);
		$("#zhuanxianTable tbody tr").remove();
		$("#zhuanxianTable tbody").append(tr1+tr2);
		$("#zhuanxianTotal").text("共 "+data.totalElements+" 种类型");
		$('#zhuanxian').highcharts({
	        chart: {
	            plotBackgroundColor: null,
	            plotBorderWidth: null,
	            plotShadow: false
	        },
	        title: {
	            text: '快车订单类型统计'
	        },
	        tooltip: {
	    	    pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
	        },
	        plotOptions: {
	            pie: {
	                allowPointSelect: true,
	                cursor: 'pointer',
	                dataLabels: {
	                    enabled: true,
	                    color: '#000000',
	                    connectorColor: '#000000',
	                    format: '<b>{point.name}</b>: {point.percentage:.1f} %'
	                }
	            }
	        },
	        series: [{
	            type: 'pie',
	            name: '完成订单量',
	            data: record
	        
	        }]
	    });
	},"json");
	
}


function freightLoad(){
	
	$.post(ctx+"/chart/freightOrderFromSourceDate",{'startTime':$("#freightStartTime").val(),'endTime':$("#freightEndTime").val(),'companyId':$("#freightCompanyId").val()},function(data){
        var record = new Array();
        var content = data.content;
        for(var i=0;i<data.totalElements;i++){
        	if(content[i].fromSource.indexOf("APP")!=-1){
        		record[i]={'name':content[i].fromSource,'y':content[i].amount/data.total,'sliced': true,'selected': true};
        	}else{
        		record[i]=[content[i].fromSource,content[i].amount/data.total];
        	}
             
        }
        
      //  load(record);
         var tr = "<tr style='font-size:14px;'><th>数量\\类型</th>";
	 	 var tr1="<tr><td>订单数量</td>";
	 	 var tr2="<tr><td>订单金额</td>";
         for(var i=0;i<data.totalElements;i++){
        	 if(i==data.totalElements-1){
        		 tr=tr+"<th>"+content[i].fromSource+"</th><th>总计</th></tr>";
        		 tr1=tr1+"<td>"+content[i].amount+" (单)</td><td>"+data.total+" (单)</td></tr>";
        		 tr2=tr2+"<td>"+content[i].money+" (元)</td><td>"+data.allTotal+" (元)</td><tr>"
        	 }else{
        		 tr=tr+"<th>"+content[i].fromSource+"</th>";
        		 tr1=tr1+"<td>"+content[i].amount+" (单)</td>";
        		 tr2=tr2+"<td>"+content[i].money+" (元)</td>"
        	 }
         }   
        $("#freightTable thead tr").remove();
		$("#freightTable thead").append(tr);
		$("#freightTable tbody tr").remove();
		$("#freightTable tbody").append(tr1+tr2);
		$("#freightTotal").text("共 "+data.totalElements+" 种类型");
		$('#freight').highcharts({
	        chart: {
	            plotBackgroundColor: null,
	            plotBorderWidth: null,
	            plotShadow: false
	        },
	        title: {
	            text: '货运订单类型统计'
	        },
	        tooltip: {
	    	    pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
	        },
	        plotOptions: {
	            pie: {
	                allowPointSelect: true,
	                cursor: 'pointer',
	                dataLabels: {
	                    enabled: true,
	                    color: '#000000',
	                    connectorColor: '#000000',
	                    format: '<b>{point.name}</b>: {point.percentage:.1f} %'
	                }
	            }
	        },
	        series: [{
	            type: 'pie',
	            name: '完成订单量',
	            data: record
	        
	        }]
	    });
	},"json");
	
}